<template>
  <div class="register-container">
    <div class="particle-container">
      <div v-for="i in 50" :key="i" class="particle"></div>
    </div>
    <div class="cyber-circle"></div>
    <div class="cyber-circle circle-2"></div>
    
    <div class="content">
      <div class="register-box">
      <div class="head">
          <div class="title-container">
            <h1>账号注册</h1>
            <div class="cyber-line"></div>
          </div>
          <router-link class="link" to="/login">
            返回登录
            <span class="cyber-arrow">←</span>
          </router-link>
      </div>

        <el-form ref="registerForm" :model="formData" :rules="rules" class="register-form">
          <div class="cyber-input-group">
            <el-form-item prop="email">
              <el-input 
                v-model="formData.email" 
                prefix-icon="el-icon-message" 
                placeholder="请输入邮箱">
              </el-input>
              <div class="cyber-line"></div>
        </el-form-item>
          </div>

          <div class="cyber-input-group">
            <el-form-item prop="code" class="code-input">
              <div class="input-with-button">
                <el-input 
                  v-model="formData.code" 
                  prefix-icon="el-icon-key" 
                  placeholder="请输入验证码">
                </el-input>
                <el-button class="code-button cyber-button" @click="getCode" :disabled="isCodeDisabled">
                  <span class="button-text">{{ codeText }}</span>
                  <div class="button-glitch"></div>
                </el-button>
              </div>
              <div class="cyber-line"></div>
        </el-form-item>
          </div>

          <div class="cyber-input-group">
            <el-form-item prop="password">
              <el-input 
                type="password" 
                v-model="formData.password" 
                prefix-icon="el-icon-lock" 
                placeholder="请输入密码">
              </el-input>
              <div class="cyber-line"></div>
        </el-form-item>
          </div>

          <div class="cyber-input-group">
            <el-form-item prop="confirmPassword">
              <el-input 
                type="password" 
                v-model="formData.confirmPassword" 
                prefix-icon="el-icon-lock" 
                placeholder="请确认密码">
              </el-input>
              <div class="cyber-line"></div>
        </el-form-item>
          </div>

          <el-form-item class="agreement">
            <el-checkbox v-model="formData.agreement">
              同意 <a href="#" class="cyber-link">《用户协议》</a> 和 <a href="#" class="cyber-link">《隐私政策》</a>
            </el-checkbox>
        </el-form-item>

        <el-form-item>
            <button @click="submitRegister" class="cyber-button submit-button" type="button">
              <span class="button-text">注 册</span>
              <div class="button-glitch"></div>
            </button>
        </el-form-item>
      </el-form>
    </div>
  </div>
  </div>
</template>

<script>
import axios from '../config/http'

export default {
  data() {
    // 确认密码的验证规则
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.formData.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    
    return {
      formData: {
        email: '',
        code: '',
        password: '',
        confirmPassword: '',
        agreement: false
      },
      isCodeDisabled: false,
      codeText: '获取验证码',
      countdown: 60,
      timer: null,
      rules: {
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
        ],
        password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请再次输入密码', trigger: 'blur' },
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 获取验证码
    async getCode() {
      if (!this.formData.email) {
        this.$message('请输入邮箱！')
        return
      }
      if (!/^[-a-zA-Z0-9_.+]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/.test(this.formData.email)) {
      this.$message('邮箱格式不正确！')
      return
      }
      this.isCodeDisabled = true
      this.startCountdown()
      try {
        let result = await axios.post('/as/accounts/captcha', {
          email: this.formData.email
        }, {
          headers: {
          'Content-Type': 'application/x-www-form-urlencoded'
          }
        })
        if(result.data.code === 200) {
          this.$message.success('验证码已发送到您的邮箱')
        } else {
          this.$message.error(result.data.msg || '验证码发送失败')
          this.isCodeDisabled = false
          clearInterval(this.timer)
          this.codeText = '获取验证码'
        }
      } catch (error) {
        this.$message.error('验证码发送失败')
        this.isCodeDisabled = false
        clearInterval(this.timer)
        this.codeText = '获取验证码'
      }
    },

    // 倒计时
    startCountdown() {
      if (this.timer) {
        clearInterval(this.timer)
      }
      this.countdown = 60
      this.timer = setInterval(() => {
        this.countdown--
        this.codeText = `${this.countdown}秒后重新获取`
        if (this.countdown === 0) {
          clearInterval(this.timer)
          this.codeText = '获取验证码'
          this.isCodeDisabled = false
        }
      }, 1000)
    },

    // 提交注册
    submitRegister() {
      if (!this.formData.agreement) {
        this.$message.warning('请阅读并同意用户协议和隐私政策')
        return
      }
      
      this.$refs.registerForm.validate(async (valid) => {
        if (valid) {
          try {
            const result = await axios.post('/as/accounts/register', {
              email: this.formData.email,
              password: this.formData.password,
              code: this.formData.code
            })
            
            if (result.data.code === 200) {
              this.$message.success('注册成功！')
              this.$router.push('/login')
            } else {
              this.$message.error(result.data.msg || '注册失败')
            }
          } catch (error) {
            this.$message.error('注册失败，请稍后重试')
          }
        }
      })
    }
  },
  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer)
    }
  }
}
</script>

<style lang="less" scoped>
.register-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000;
  position: relative;
  overflow: hidden;

  &::before {
    content: '';
    position: absolute;
    width: 200%;
    height: 200%;
    top: -50%;
    left: -50%;
    background: radial-gradient(circle at center, transparent 0%, #000 70%),
                repeating-linear-gradient(transparent 0%, transparent 2px, rgba(0, 255, 157, 0.1) 3px, transparent 3px, transparent 9px),
                repeating-linear-gradient(90deg, transparent 0%, transparent 2px, rgba(0, 255, 157, 0.1) 3px, transparent 3px, transparent 9px);
    animation: matrix 20s linear infinite;
    opacity: 0.2;
  }

  &::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 2px;
    background: linear-gradient(90deg, transparent, #00ff9d, transparent);
    animation: scan 3s linear infinite;
  }

  .particle-container {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 0;
    
    .particle {
      position: absolute;
      width: 2px;
      height: 2px;
      background: #00ff9d;
      border-radius: 50%;
      animation: float 4s infinite, glow 2s infinite alternate;
      opacity: 0.5;
      box-shadow: 0 0 10px #00ff9d, 0 0 20px #00ff9d;
      
      .particle-position(@i, @x, @y, @delay, @duration) {
        &:nth-child(@{i}) {
          left: @x;
          top: @y;
          animation-delay: @delay;
          animation-duration: @duration;
        }
      }

      .particle-position(1, 10%, 20%, 0.2s, 4s);
      .particle-position(2, 20%, 40%, 0.4s, 4.5s);
      .particle-position(3, 30%, 60%, 0.6s, 5s);
      .particle-position(4, 40%, 80%, 0.8s, 4.2s);
      .particle-position(5, 50%, 20%, 1s, 4.8s);
      .particle-position(6, 60%, 40%, 1.2s, 5.2s);
      .particle-position(7, 70%, 60%, 1.4s, 4.3s);
      .particle-position(8, 80%, 80%, 1.6s, 4.7s);
      .particle-position(9, 90%, 20%, 1.8s, 5.1s);
      .particle-position(10, 15%, 40%, 2s, 4.4s);
      .particle-position(11, 25%, 60%, 2.2s, 4.9s);
      .particle-position(12, 35%, 80%, 2.4s, 5.3s);
      .particle-position(13, 45%, 20%, 2.6s, 4.1s);
      .particle-position(14, 55%, 40%, 2.8s, 4.6s);
      .particle-position(15, 65%, 60%, 3s, 5s);
      .particle-position(16, 75%, 80%, 3.2s, 4.5s);
      .particle-position(17, 85%, 20%, 3.4s, 4.8s);
      .particle-position(18, 95%, 40%, 3.6s, 5.2s);
      .particle-position(19, 5%, 60%, 3.8s, 4.3s);
      .particle-position(20, 15%, 80%, 4s, 4.7s);
    }
  }

  .cyber-circle {
    position: absolute;
  width: 600px;
    height: 600px;
    border: 2px solid rgba(0, 255, 157, 0.1);
    border-radius: 50%;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    animation: rotate 20s linear infinite;
    
    &::after {
      content: '';
      position: absolute;
      width: 100%;
      height: 100%;
      border: 2px solid rgba(0, 255, 157, 0.2);
      border-radius: 50%;
      animation: pulse 2s ease-out infinite;
    }

    &::before {
      content: '';
      position: absolute;
      width: 120%;
      height: 120%;
      border: 1px solid rgba(0, 255, 157, 0.1);
      border-radius: 50%;
      animation: pulse 2s ease-out infinite 1s;
    }

    &.circle-2 {
      width: 800px;
      height: 800px;
      animation-direction: reverse;
      animation-duration: 30s;
    }
  }

  .content {
    position: relative;
    z-index: 1;
    width: 460px;
    padding: 40px;
    background: rgba(13, 17, 23, 0.85);
    border-radius: 12px;
    box-shadow: 0 0 30px rgba(0, 255, 157, 0.2),
                inset 0 0 15px rgba(0, 255, 157, 0.1);
    backdrop-filter: blur(20px);
    border: 1px solid rgba(0, 255, 157, 0.3);
    animation: contentGlow 3s infinite alternate;

    &::before {
      content: '';
      position: absolute;
      top: -2px;
      left: -2px;
      right: -2px;
      bottom: -2px;
      background: linear-gradient(45deg, #00ff9d, transparent, #00ff9d, transparent);
      border-radius: 12px;
      z-index: -1;
      animation: borderGlow 3s linear infinite;
    }
  }

  .register-box {
    .head {
      text-align: center;
      margin-bottom: 30px;
      position: relative;

      .title-container {
        position: relative;
        display: inline-block;
        
        h1 {
          font-size: 28px;
          color: #fff;
          margin: 0 0 20px;
          font-weight: 500;
          text-shadow: 0 0 10px rgba(0, 255, 157, 0.5);
          letter-spacing: 2px;
          animation: textGlow 2s infinite alternate;

          &::before {
            content: attr(data-text);
            position: absolute;
            left: 2px;
            text-shadow: -1px 0 #00ff9d;
            top: 0;
            color: #fff;
            background: #000;
            overflow: hidden;
            animation: glitch 3s infinite linear alternate-reverse;
          }
        }

        .cyber-line {
          position: absolute;
          bottom: 0;
          left: 0;
          width: 100%;
          height: 2px;
          background: linear-gradient(90deg, transparent, #00ff9d, transparent);
          animation: scan 2s linear infinite;
        }
      }

      .link {
        position: absolute;
        right: 0;
        top: 50%;
        transform: translateY(-50%);
        color: #00ff9d;
        font-size: 14px;
        text-decoration: none;
        transition: all 0.3s;
        display: flex;
        align-items: center;
        
        .cyber-arrow {
          margin-left: 5px;
    font-size: 18px;
          animation: blink 1s infinite;
        }

        &:hover {
          text-shadow: 0 0 10px #00ff9d;
        }
      }
    }

    .register-form {
      .cyber-input-group {
        position: relative;
        margin-bottom: 20px;

        .el-input {
          .el-input__inner {
            height: 44px;
            background: rgba(0, 255, 157, 0.05);
            border: 1px solid rgba(0, 255, 157, 0.2);
            color: #fff;
            transition: all 0.3s;

            &:focus {
              box-shadow: 0 0 15px rgba(0, 255, 157, 0.2);
              border-color: #00ff9d;
            }
          }
        }
      }

      .code-input {
        .input-with-button {
          display: flex;
          align-items: center;
          gap: 10px;
          width: 100%;

  .el-input {
            flex: 1;
          }
      
          .code-button {
            width: 120px;
      height: 44px;
            margin: 0;
            padding: 0;
            white-space: nowrap;
          }
        }
      }

      .cyber-input-group {
        position: relative;
        margin-bottom: 20px;

        .el-form-item {
          margin-bottom: 0;
        }

        .el-input {
      .el-input__inner {
            height: 44px;
            background: rgba(0, 255, 157, 0.05);
            border: 1px solid rgba(0, 255, 157, 0.2);
            color: #fff;
            transition: all 0.3s;
          }
        }
      }

      .cyber-button {
        position: relative;
        overflow: hidden;
        transition: all 0.3s;
        border: 1px solid #00ff9d;
        background: transparent;
        
        &::before {
          content: '';
          position: absolute;
          top: 0;
          left: -100%;
          width: 100%;
          height: 100%;
          background: linear-gradient(90deg, transparent, rgba(0, 255, 157, 0.2), transparent);
          animation: btnGlow 3s infinite;
        }

        &:hover {
          box-shadow: 0 0 20px rgba(0, 255, 157, 0.4);
          text-shadow: 0 0 5px #00ff9d;
          background: rgba(0, 255, 157, 0.1);
        }
      }

      .agreement {
        margin: 20px 0;
        color: rgba(255, 255, 255, 0.7);

        .cyber-link {
          color: #00ff9d;
          text-decoration: none;
          position: relative;
          
          &::after {
            content: '';
            position: absolute;
            bottom: -2px;
            left: 0;
            width: 100%;
            height: 1px;
            background: #00ff9d;
            transform: scaleX(0);
            transition: transform 0.3s;
      }

          &:hover::after {
            transform: scaleX(1);
          }
        }
      }
    }
  }
}

@keyframes matrix {
  0% {
    transform: translateY(0) rotate(0deg);
  }
  100% {
    transform: translateY(-50%) rotate(45deg);
  }
}

@keyframes scan {
  0% {
    transform: translateY(0) scaleX(0);
    opacity: 0;
  }
  50% {
    transform: translateY(50vh) scaleX(1);
    opacity: 1;
  }
  100% {
    transform: translateY(100vh) scaleX(0);
    opacity: 0;
  }
}

@keyframes glow {
  0% {
    box-shadow: 0 0 5px #00ff9d;
  }
  100% {
    box-shadow: 0 0 20px #00ff9d, 0 0 30px #00ff9d;
  }
}

@keyframes contentGlow {
  0% {
    box-shadow: 0 0 20px rgba(0, 255, 157, 0.2);
  }
  100% {
    box-shadow: 0 0 40px rgba(0, 255, 157, 0.4);
  }
}

@keyframes borderGlow {
  0% {
    opacity: 0;
    transform: rotate(0deg);
  }
  50% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform: rotate(360deg);
  }
}

@keyframes textGlow {
  0% {
    text-shadow: 0 0 5px #00ff9d;
  }
  100% {
    text-shadow: 0 0 15px #00ff9d, 0 0 25px #00ff9d;
  }
}

@keyframes glitch {
  0% {
    clip: rect(44px, 450px, 56px, 0);
  }
  5% {
    clip: rect(12px, 450px, 76px, 0);
  }
  10% {
    clip: rect(54px, 450px, 96px, 0);
  }
  15% {
    clip: rect(34px, 450px, 46px, 0);
  }
  20% {
    clip: rect(64px, 450px, 86px, 0);
  }
  25% {
    clip: rect(14px, 450px, 26px, 0);
  }
  30% {
    clip: rect(84px, 450px, 96px, 0);
  }
  35% {
    clip: rect(4px, 450px, 16px, 0);
  }
  40% {
    clip: rect(74px, 450px, 86px, 0);
  }
  45% {
    clip: rect(24px, 450px, 36px, 0);
  }
  50% {
    clip: rect(94px, 450px, 106px, 0);
  }
}

@keyframes btnGlow {
  0% {
    left: -100%;
  }
  100% {
    left: 100%;
  }
}

@keyframes blink {
  0%, 100% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
}

@media screen and (max-width: 768px) {
  .register-container {
    .content {
      width: 90%;
      margin: 20px;
      padding: 20px;
    }

    .cyber-circle {
      display: none;
    }
  }
}
</style>